<!--药品类管理-->
<template>
  <div style="background-color: #fff;padding: 10px;margin: 5px auto;width: 85%;align-items: center;display: flex;justify-content: center;">
    <span style="line-height: 50px;font-size: 30px;color: lightseagreen">药品类管理</span>
  </div>
<!--  药品项搜索卡片-->
  <el-card style="margin: 5px auto;height: 70px;width: 88%">
    <el-form :inline="true">
      <el-form-item label="药品类名称">
        <el-input placeholder="请输入" style="width:220px;" v-model="searchForm.name"
                  @keydown.enter="selectMedicine"></el-input>
      </el-form-item>
      <el-form-item label="药品类编码">
        <el-input placeholder="请输入" style="width:220px;" v-model="searchForm.code"
                 @keydown.enter="selectMedicine"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="success" @click="selectMedicine" >搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>

<!--    药品类列表-->
    <el-card style="margin: 1px auto;width: 88%;overflow-y:auto;">
      <div style="margin:2px 0px 40px 10px;">
        药品类列表
        <el-button type="success" style="float: right;" @click="addMedicine">新增药品项</el-button>
      </div>
      <el-table :data="medicineArr">
        <el-table-column type="index" label="编号" width="80" align="center"/>
        <el-table-column prop="name" label="药品类名称" align="center"/>
        <el-table-column prop="code" label="药品类编码" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="showMedicineOption(scope.row.id)">{{scope.row.code}}</el-button>
        </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注信息" align="center"/>
        <el-table-column prop="createTime" label="创建时间" align="center"/>
        <el-table-column  label="操作" align="center">
          <template #default="scope">
          <el-button link type="primary" size="small" @click="editMedicine(scope.row.id)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteMedicine(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin:30px;float: right;" >
        <!--layout="total(总数), sizes(每页总数), prev(上一页), pager(当前页), next(下一页), jumper(跳转页)"
         :pager-count="4" 表示显示4个页码
         :page-sizes="[5, 10, 15, 20]" 表示一共有几个"每页显示多少条"选项
         current-page当前页码 page-size当前页码的条数 total总条数 这三个变量需要和实际数据双向绑定
         :total="400" 表示总条数，需要双向绑定
         @size-change="handleSizeChange" 表示每页显示多少条改变时触发的事件
         @current-change="handleCurrentChange" 表示当前页码改变时触发的事件 -->
        <el-pagination
            layout="total, sizes, prev, pager, next, jumper"
            :page-sizes="[5, 10, 15, 20]"
            v-model:current-page="currentPage"
            v-model:page-size="currentSize"
            v-model:total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

<!--      保存药品项弹窗-->
      <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
                 :before-close="handleClose">
        <el-form label-width="80px" label-position="top">
          <el-form-item label="药品类名称">
            <el-input placeholder="请输入" v-model="saveMedicineFrom.name"></el-input>
          </el-form-item>
          <el-form-item label="药品类编码">
            <el-input placeholder="请输入" v-model="saveMedicineFrom.code"></el-input>
          </el-form-item>
          <el-form-item label="备注信息">
            <el-input placeholder="请输入" v-model="saveMedicineFrom.remark"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="saveMedicine" >保存</el-button>
        </template>
      </el-dialog>




</template>
<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";
import router from "@/router";

//定义变量保存分页器的当前页数
const currentPage = ref(1);
//定义变量保存分页器的每页显示多少条
const currentSize = ref(5);
//定义变量保存数据总条目数
const total = ref(100);
//修改每页显示多少条，触发此函数
const handleSizeChange = (val) => {
  console.log('每页展示' + val + '条数据');
  selectMedicine();
}
//修改当前页码，触发此函数
const handleCurrentChange = (val) => {
  console.log('当前的页数为：' + val);
  selectMedicine();
}

// 定义对象保存搜索条件
const searchForm = ref({
  name:'',
  code:''
});
// 定义数组保存药品类列表数据
const medicineArr = ref([]);
//定义加载药品类的方法
const selectMedicine = ()=>{
  searchForm.value.pageSize = currentSize.value;
  searchForm.value.pageNum = currentPage.value;
  let data = qs.stringify(searchForm.value);
  axios.get(BASE_URL+'/v1/medicine/select?'+data).then((response)=>{
    if(response.data.code == 2000){
      medicineArr.value = response.data.data.list;
      total.value = response.data.data.total
    }else{
      ElMessage.error('没有搜索到相关数据');
      medicineArr.value = [];
    }
  })
}
onMounted(()=>{
  selectMedicine();
})
//定义重置搜索的方法
const resetSearch = ()=>{
  searchForm.value = {name:'',code:''};
  selectMedicine();
}


//定义变量保存弹窗的标题
const dialogTitle = ref('新增药品项')
// 定义变量控制弹窗是否可见
const dialogVisible = ref(false)
//定义对象用来保存新增药品类的表单值
const saveMedicineFrom = ref({
  name:'',
  code:'',
  remark:''
});
// 定义保存药品类的方法
const saveMedicine = ()=>{
  let data = qs.stringify(saveMedicineFrom.value)
  axios.post(BASE_URL + '/v1/medicine/save',data)
      .then((response)=>{
        if (response.data.code == 2000){
          ElMessage.success('药品类数据操作成功')
          dialogVisible.value = false;
          selectMedicine();
        }else {
          ElMessage.error(response.data.msg)
        }
      })
}
// 定义新增药品类的方法
const addMedicine = ()=>{
  dialogTitle.value = '新增药品项';
  dialogVisible.value = true;
}

//定义处理弹窗关闭的方法
const handleClose = ()=> {
  if (confirm('是否要关闭本弹窗?')) {
    dialogVisible.value = false;
    saveMedicineFrom.value = {};
  }
}

// 定义编辑药品类的方法
const editMedicine = (id)=>{
  dialogVisible.value = true;
  dialogTitle.value = '编辑药品类'
  axios.get(BASE_URL + '/v1/medicine/select?id='+id)
      .then((response)=>{
        if (response.data.code == 2000){
          saveMedicineFrom.value = response.data.data.list[0];
        }else {
          ElMessage.error(response.data.msg)
        }
      })

}

// 定义根据药品类id加载到对应的药品项的方法
const showMedicineOption = (id)=>{
  router.push("/medicineOption?id="+id)
}

//定义删除药品类的方法
const deleteMedicine = (id)=>{
    axios.post(BASE_URL + '/v1/medicine/delete/'+id)
        .then((response)=>{
          if (response.data.code == 2000){
            ElMessage.success('删除药品类成功');
            selectMedicine();
          }else {
            ElMessage.error(response.data.msg);
          }
        })
}

</script>


<style scoped>

</style>